home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / m68k / cc68k.arc / MEMMGT.C < prev    next >
C/C++ Source or Header  |  1986-10-28  |  4KB  |  128 lines

  1. #include        "stdio.h"
  2. #include        "malloc.h"
  3. #include        "c.h"
  4. #include        "expr.h"
  5. #include        "gen.h"
  6. #include        "cglbdec.h"
  7.  
  8. /*
  9.  *    68000 C compiler
  10.  *
  11.  *    Copyright 1984, 1985, 1986 Matthew Brandt.
  12.  *  all commercial rights reserved.
  13.  *
  14.  *    This compiler is intended as an instructive tool for personal use. Any
  15.  *    use for profit without the written consent of the author is prohibited.
  16.  *
  17.  *    This compiler may be distributed freely for non-commercial use as long
  18.  *    as this notice stays intact. Please forward any enhancements or question
  19. s
  20.  *    to:
  21.  *
  22.  *        Matthew Brandt
  23.  *        Box 920337
  24.  *        Norcross, Ga 30092
  25.  */
  26.  
  27. struct blk {
  28.         struct blk      *next;
  29.         char            m[1];           /* memory area */
  30.         };
  31.  
  32. static int      glbsize = 0,    /* size left in current global block */
  33.                 locsize = 0,    /* size left in current local block */
  34.                 glbindx = 0,    /* global index */
  35.                 locindx = 0;    /* local index */
  36.  
  37. static struct blk       *locblk = 0,    /* pointer to local block */
  38.                         *glbblk = 0;    /* pointer to global block */
  39.  
  40. char    *xalloc(siz)
  41. int     siz;
  42. {       struct blk      *bp;
  43.         char            *rv;
  44.         int             exit();
  45.     if( siz & 1 )        /* if odd size */
  46.         siz += 1;    /* make it even */
  47.         if( global_flag ) {
  48.                 if( glbsize >= siz ) {
  49.                         rv = &(glbblk->m[glbindx]);
  50.                         glbsize -= siz;
  51.                         glbindx += siz;
  52.                         return rv;
  53.                         }
  54.                 else    {
  55.                         bp =(struct blk *)(calloc((unsigned)1,
  56.                                                    (unsigned)(sizeof(struct blk) + 2047)));
  57.             if( bp == NULL )
  58.             {
  59.                 printf(" not enough memory.\n");
  60.                 exit(1);
  61.             }
  62.                         bp->next = glbblk;
  63.                         glbblk = bp;
  64.                         glbsize = 2048 - siz;
  65.                         glbindx = siz;
  66.                         return glbblk->m;
  67.                         }
  68.                 }
  69.         else    {
  70.                 if( locsize >= siz ) {
  71.                         rv = &(locblk->m[locindx]);
  72.                         locsize -= siz;
  73.                         locindx += siz;
  74.                         return rv;
  75.                         }
  76.                 else    {
  77.                         bp =(struct blk *)(calloc((unsigned)1,
  78.                                                   (unsigned)(sizeof(struct blk) + 2047)));
  79.             if( bp == NULL )
  80.             {
  81.                 printf(" not enough local memory.\n");
  82.                 exit(1);
  83.             }
  84.                         bp->next = locblk;
  85.                         locblk = bp;
  86.                         locsize = 2048 - siz;
  87.                         locindx = siz;
  88.                         return locblk->m;
  89.                         }
  90.                 }
  91. }
  92.  
  93.  release_local()
  94. {       struct blk      *bp1, *bp2;
  95.         int             blkcnt;
  96.         blkcnt = 0;
  97.         bp1 = locblk;
  98.         while( bp1 != 0 ) {
  99.                 bp2 = bp1->next;
  100.                 free((char *)bp1 );
  101.                 ++blkcnt;
  102.                 bp1 = bp2;
  103.                 }
  104.         locblk = 0;
  105.         locsize = 0;
  106.         lsyms.head = 0;
  107.         printf(" releasing %d bytes local tables.\n",blkcnt * 2048);
  108. }
  109.  
  110.  release_global()
  111. {       struct blk      *bp1, *bp2;
  112.         int             blkcnt;
  113.         bp1 = glbblk;
  114.         blkcnt = 0;
  115.         while( bp1 != 0 ) {
  116.                 bp2 = bp1->next;
  117.                 free((char *)bp1);
  118.                 ++blkcnt;
  119.                 bp1 = bp2;
  120.                 }
  121.         glbblk = 0;
  122.         glbsize = 0;
  123.         gsyms.head = 0;         /* clear global symbol table */
  124.         printf(" releasing %d bytes global tables.\n",blkcnt * 2048);
  125.         strtab = 0;             /* clear literal table */
  126. }
  127.  
  128.